package net.toocruel.iqismart.entity.core;

import net.toocruel.iqismart.entity.base.BaseEntity;
import net.toocruel.iqismart.entity.constants.ReviewStatus;
import net.toocruel.iqismart.entity.rbac.User;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/8 上午11:35
 */
@Entity
public class Answer extends BaseEntity {
    private static final long serialVersionUID = -441764001207695553L;
    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,optional = false)
    @JoinColumn(name = "userId")
    private User user;
    private String questionTitle;
    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,optional = false)
    @JoinColumn(name = "questionId")
    private Question question;
    @NotBlank
    @Length(min = 10,max = 10000)
    private String content;

    @OneToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,mappedBy = "answer")
    private Set<Support> supports;
    @Transient
    private Boolean support;
    private int oppositions;
    private int supportNum;

    @OneToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,mappedBy = "answer")
    private Set<Comment> comments;
    private byte device;

    @Enumerated(EnumType.ORDINAL)
    private ReviewStatus status;
    private Date adoptedAt;

    @Transient
    private Boolean follow;

    public int getSupportNum() {
        return supportNum;
    }

    public void setSupportNum(int supportNum) {
        this.supportNum = supportNum;
    }

    public Boolean getSupport() {
        User currentUser = null;
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if(principal instanceof User){
                currentUser = (User) principal;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(CollectionUtils.isNotEmpty(supports)){
            User finalCurrentUser = currentUser;
            supports.forEach(sp -> {
                if(sp.getUser() == finalCurrentUser){
                    support = true;
                }
            });
        }
        return support;
    }

    public void setSupport(Boolean support) {
        this.support = support;
    }

    public Boolean getFollow() {
        return follow;
    }

    public void setFollow(Boolean follow) {
        this.follow = follow;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public String getQuestionTitle() {
        return questionTitle;
    }

    public void setQuestionTitle(String questionTitle) {
        this.questionTitle = questionTitle;
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Set<Support> getSupports() {
        return supports;
    }

    public void setSupports(Set<Support> supports) {
        this.supports = supports;
    }

    public int getOppositions() {
        return oppositions;
    }

    public void setOppositions(int oppositions) {
        this.oppositions = oppositions;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Set<Comment> getComments() {
        return comments;
    }

    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }

    public byte getDevice() {
        return device;
    }

    public void setDevice(byte device) {
        this.device = device;
    }

    public ReviewStatus getStatus() {
        return status;
    }

    public void setStatus(ReviewStatus status) {
        this.status = status;
    }

    public Date getAdoptedAt() {
        return adoptedAt;
    }

    public void setAdoptedAt(Date adoptedAt) {
        this.adoptedAt = adoptedAt;
    }
}
